package com.example.springboot.service.impl;

import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.WithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class WithdrawalServiceImpl implements WithdrawalService {
    @Autowired
    private WithdrawalMapper withdrawalMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public List<Withdrawal> findAll() {
        return withdrawalMapper.findAll();
    }

    @Override
    public Withdrawal findById(Integer id) {
        return withdrawalMapper.findById(id);
    }

    @Override
    public int insert(Withdrawal withdrawal) {
        Store store = null;
        try {
            // 判断是否有这么多钱
            Integer storeId = withdrawal.getStoreId();
            store = storeMapper.selectById(storeId);
        } catch (Exception e) {
            throw new ServiceException("无该商家");
        }
        if (store.getSurplus().compareTo(new BigDecimal(withdrawal.getAmount())) < 0) {
            throw new ServiceException("余额不足");
        }
        try {
            return withdrawalMapper.insert(withdrawal);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("提现失败");
        }


    }

    @Transactional
    @Override
    public int update(Withdrawal withdrawal) {


        if (withdrawal.getStatus() == 3 || withdrawal.getStatus() == 1) {
            int update = withdrawalMapper.update(withdrawal);
            return update;
        }
        Store store = null;
        try {
            // 判断是否有这么多钱
            Integer storeId = withdrawal.getStoreId();
            store = storeMapper.selectById(storeId);
        } catch (Exception e) {
            throw new ServiceException("无该商家");
        }
        if (store == null) {
            throw new ServiceException("无该商家");
        }
        if (store.getSurplus().compareTo(new BigDecimal(withdrawal.getAmount())) < 0) {
            // 余额不足
            withdrawal.setStatus(3);
            int update = withdrawalMapper.update(withdrawal);
            return update;
        }
        try {
            int update = withdrawalMapper.update(withdrawal);
            if (update == 0) {
                throw new ServiceException("更新失败");
            }
            if (update == 1) {
                // 更新商家余额
                store.setSurplus(store.getSurplus().subtract(new BigDecimal(withdrawal.getAmount())));
                storeMapper.updateById(store);
            }
            return update;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("更新失败");
        }
    }

    @Override
    public int delete(Integer id) {
        return withdrawalMapper.delete(id);
    }

    @Override
    public void deleteList(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                // 删除数据
                withdrawalMapper.delete(id);
            }
        } catch (Exception e) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public Page<Withdrawal> selectByManyCondition(Withdrawal withdrawal, Integer currentPage, Integer pageSize) {
        try {
            Integer startRow = (currentPage - 1) * pageSize;
            List<Withdrawal> withdrawals = withdrawalMapper.selectByManyCondition(withdrawal, startRow, pageSize);
            startRow = null;
            pageSize = null;
            List<Withdrawal> withdrawalTotal = withdrawalMapper.selectByManyCondition(withdrawal, startRow, pageSize);
            int total = withdrawalTotal.size();
            Page<Withdrawal> page = new Page();
            page.setList(withdrawals);
            page.setTotal(total);
            return page;
        } catch (Exception e) {
            throw new ServiceException("查询失败");
        }
    }
}
